// OGoMojo
// Whitemice.MOGI.RPC2.Contact
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using System.Globalization;
using Whitemice.MOGI;
using CookComputing.XmlRpc;

namespace Whitemice.MOGI.RPC2 
{
	/// \brief Implementation of IContact
	public class Contact : Whitemice.MOGI.RPC2.Company, IContact 
	{
		/// \brief Constructor
		/// \param document An XmlRpcStruct document
		/// \param server Reference to server object		
		public Contact(XmlRpcStruct document, Server server)
		 : base(document, server) 
		{
		}
		
		/// \brief Returns the unique name of the object
		public override string Name 
		{
			get 
			{
				return ("Document/Contact/" + Number);
			}
		}
		
		/// \brief Accessor for the firstname value
		public string FirstName 
		{
			get 
			{
				if(document.ContainsKey("firstname"))
					return (string)document["firstname"];
				return "";
			}
			set 
			{
				if (document.ContainsKey("firstname"))
					document["firstname"] = value;
					else document.Add("firstname", value);
			}
		}
		
		/// \brief Accessor for the name value
		public string LastName 
		{
			get 
			{
				if (document.ContainsKey("name"))
					return (string)document["name"];
				return "";
			}
			set 
			{
				if (document.ContainsKey("name"))
					document["name"] = value;
					else document.Add("name", value);
			}
		}
		
		/// \brief Accessor for the nickname value
		public string DisplayName 
		{
			get 
			{
				if (document.ContainsKey("nickname"))
					return (string)document["nickname"];
				return "";
			}
			set 
			{
				if (document.ContainsKey("nickname"))
					document["nickname"] = value;
					else document.Add("nickname", value);
			}
		}
		
		/// \brief Accessor for the middlename value
		public string MiddleName 
		{
			get 
			{
				if(document.ContainsKey("middlename"))
					return (string)document["middlename"];
				return "";
			}
			set 
			{
				if (document.ContainsKey("middlename"))
					document["middlename"] = value;
					else document.Add("middlename", value);
			} 
		}
		
		/// \brief Accessor for the birthday value
		public DateTime BirthDate 
		{
			get 
			{
				if (document.ContainsKey("birthday"))
					if (document["birthday"] == null)
						throw new NoValueException();
						else return (DateTime)document["birthday"];
					else throw new NoKeyException();
			}
			set 
			{
				if (document.ContainsKey("birthday"))
					document["birthday"] = value;
					else document.Add("birthday", value);
			}
		}
		
		/// \brief Accessor for the degree value
		public string Degree 
		{
			get 
			{
				if(document.ContainsKey("degree"))
					return (string)document["degree"];
				return "";
			}
			set 
			{
				if (document.ContainsKey("degree"))
					document["degree"] = value;
					else document.Add("degree", value);
			}
		}
		
		/// \brief Accessor for the salutation value
		public Salutation Salutation 
		{ 
			get 
			{
				Salutation salutation;
				salutation = Salutation.Undefined;
				switch((string)document["salutation"]) 
				{
       				case "01_dear_ms":
         				salutation = Salutation.Ms;
        				break;
					case "02_dear_mrs":
						salutation = Salutation.Mrs;
						break;
					case "03_dear_mr":
						salutation = Salutation.Mr;
						break;
					case "04_dear_prof":
						salutation = Salutation.Prof;
						break;
					case "05_dear_dr":
						salutation = Salutation.Dr;
						break;
				}
				return salutation;
			}
			set 
			{
				switch(value) 
				{
					case Salutation.Ms:
						document["salutation"] = "01_dear_ms";
						break;
					case Salutation.Mrs:
						document["salutation"] = "02_dear_mrs";
						break;
					case Salutation.Mr:
						document["salutation"] = "03_dear_mr";
						break;
					case Salutation.Prof:
						document["salutation"] = "04_dear_prof";
						break;
					case Salutation.Dr:
						document["salutation"] = "05_dear_dr";
						break;
				}
			}
		}
		
		//// \brief Accessor for the gender value
		public Gender Gender 
		{
			get 
			{
				Gender gender;
				gender = Gender.Undefined;
				if (document.ContainsKey("gender"))
					switch((string)document["gender"]) 
					{
						case "undefined":
								gender = Gender.Undefined;
							break;
						case "male":
								gender = Gender.Male;
							break;
						case "female":
								gender = Gender.Female;
							break;
					}
				return gender;
			}
			set 
			{
				switch(value) 
				{
					case Gender.Male:
						document["gender"] = "male";
						break;
					case Gender.Female:
						document["gender"] = "female";
						break;
					case Gender.Undefined:
						document["gender"] = "undefined";
						break;
				}
			}
		}
		
		public string Email1
		{
			get
			{
				if(HasExtendedAttribute("email1"))
					return (string)ExtendedAttribute("email1");
				return "";
			}
			set
			{
				ExtendedAttribute("email1", value);
			}
		}
		
		public string Login
		{
			get
			{
				if (document.ContainsKey("login"))
					return (string)document["login"];
				return "";
			}
		}
		
		public string JobTitle
		{
			get
			{
				if(HasExtendedAttribute("job_title"))
					return (string)ExtendedAttribute("job_title");
				return "";
			}
			set
			{
				ExtendedAttribute("job_title", value);
			}
		}
		
		public string OtherTitle1
		{
			get
			{
				if(HasExtendedAttribute("other_title1"))
					return (string)ExtendedAttribute("other_title1");
				return "";
			}
			set
			{
				ExtendedAttribute("other_title1", value);
			}
		}
		
		public string OtherTitle2
		{
			get
			{
				if(HasExtendedAttribute("other_title2"))
					return (string)ExtendedAttribute("other_title2");
				return "";
			}
			set
			{
				ExtendedAttribute("other_title2", value);
			}
		}
		
		internal bool IsAccount
		{
			get
			{
				if(document.Contains("isAccount"))
					if(Convert.ToBoolean(document["isAccount"]))
						return true;
				return false;
			}
		}
	}
}
	